Repository navigation
Rewrite as a real packet sniffer: framed stream, kernel BPF, pcap on both ends - #1
Merged
Merged
Conversation
The old code sent pipe-delimited text lines over TCP: nothing downstream could parse them, the client fell out of sync when a line straddled a recv(), and there was no filter, no .pcap, no back pressure. Now: * Binary wire protocol (docs/PROTOCOL.md): length-prefixed frames, magic + version, HELLO/PACKET/STATS/BYE/ERR. The incremental reader handles TCP segmentation, so the client cannot break on a split frame. * Capture: AF_PACKET with promiscuous mode, kernel BPF filters compiled with libpcap (optional at build time), kernel receive timestamps, drop accounting, and the control connection excluded from the capture by default so streaming over a captured interface does not feed back on itself. * Server: single epoll loop, non-blocking fan-out to N clients, per-client frame-atomic queue that only ever drops wholly-unsent frames (dropping a partly-sent frame is what desynchronised clients under load). * Dissection: Ethernet/VLAN/QinQ, SLL, BSD loopback, raw IP; ARP, IPv4, IPv6 with extension headers; TCP (flags/seq/ack/MSS), UDP, ICMP, ICMPv6; plus DNS names, TLS SNI and HTTP request lines. Every field is bounds-checked. * Client: structured display filter with AND/OR/negation and CIDR, hexdump, NDJSON for jq, .pcap export, exact --count/--duration, reconnect with backoff, colour auto-detection. * .pcap output on both ends (Wireshark-openable), with size rotation. * Proper project layout: include/, src/, tests/, docs/, scripts/, packaging/; Makefile with unit/asan/install targets; systemd unit; CI. * Tests: unit tests for framing, dissection (every truncation offset), filter grammar and the send queue under partial writes and drops; plus a live root-only e2e suite. Verified clean under ASan/UBSan and in a 700 MB/s soak with 63k dropped frames and no desync.
Same shape as ascii.rest and nodeny: banner from ascii.rest, badges, a one-line pitch, a real transcript, then Why / Install / Run it, and option tables instead of walls of flags. Adds CONTRIBUTING.md with the rules that actually matter here (never trust a length, keep the stream frame-atomic, server stays dumb) and points every link at a file that exists.
CI caught this: excluding the stream's own connection synthesises a BPF expression, so a build without libpcap could not open the capture at all and every run died at startup. Only synthesise the exclusion when we can compile it, and note the consequence at debug level otherwise. e2e.sh now detects libpcap and skips the kernel-filter checks instead of failing, so it can run in both CI matrix legs.
bas3line
added a commit
that referenced
this pull request
Nov 15, 2026
Rewrite as a real packet sniffer: framed stream, kernel BPF, pcap on both ends
bas3line
added a commit
that referenced
this pull request
Nov 15, 2026
Rewrite as a real packet sniffer: framed stream, kernel BPF, pcap on both ends
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The old
sniffer/sent pipe-delimited text lines over TCP. Nothing downstream could parse them, the client fell out of sync whenever a line straddled arecv(), the hexdump was cut at 64 bytes and squashed onto one line, and there were no filters and no.pcap, so the output was only useful if you happened to be staring at it.This replaces it with a project that is meant to be used:
include/,src/,tests/,docs/,scripts/,packaging/, a Makefile with real targets, unit tests, an e2e suite, CI, a systemd unit, and a README in the same shape as ascii.rest and nodeny.What it does now
docs/PROTOCOL.md) — length-prefixed frames with magic/version:HELLO,PACKET,STATS,BYE,ERR. The client has an incremental reader, so a frame split across TCP segments cannot break it.AF_PACKETwith promiscuous mode, kernel BPF compiled through libpcap (pcap_compile→SO_ATTACH_FILTER, optional at build time),SO_TIMESTAMPNSkernel timestamps,PACKET_STATISTICSdrop accounting. The control connection is excluded from the capture by default so streaming overlodoes not feed back on itself.or/|,!negation and CIDR; hexdump; NDJSON forjq;.pcapexport; exact--count/--duration; reconnect with backoff; colour only on a tty..pcapon both ends, with size rotation, readable bytcpdump/Wireshark/tshark.Two real bugs found while testing
ps_outbuf_pushgave up as soon as one drop failed, even after emptying the queue, leaving the queue permanently wedged at "full". Both are now regression-tested.Verified
make test— 200+ checks: framing fed one byte at a time, dissection cut at every truncation offset, filter grammar, and the send queue under short writes and a stuck reader (re-parses the delivered byte stream to prove every frame arrives whole and in order).make asan— clean under ASan + UBSan.sudo tests/e2e.sh— 25 live checks on loopback, all green:.pcapre-read bytcpdump, BPF filtering, a 64 KiB client buffer with no desync, JSON validity,--countexactness, client rejection at--max-clients, clean SIGTERM shutdown.-Werror; also builds without libpcap, where--filterfails at startup with a message that explains why instead of being silently ignored.